package com.bjgzy.courselive.mvp.ui.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bjgzy.courselive.di.component.DaggerCourseCommentComponent;
import com.bjgzy.courselive.di.module.CourseCommentModule;
import com.bjgzy.courselive.mvp.contract.CourseCommentContract;
import com.bjgzy.courselive.mvp.presenter.CourseCommentPresenter;
import com.bjgzy.module_couselive.R;
import com.bjgzy.module_couselive.R2;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.entity.CourseCommentData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import org.simple.eventbus.Subscriber;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-1 10:16:37
 */
@Route(path = RouterHub.COURSELIVE_COURSECOMMENTFRAGMENT)
public class CourseCommentFragment extends BaseFragment<CourseCommentPresenter> implements CourseCommentContract.View {
    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.tv_comment_no)
    TextView tvCommentNo;
    @BindView(R2.id.ratingBar)
    RatingBar ratingBar;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;


    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    List<CourseCommentData> lists;
    @Inject
    BaseQuickAdapter<CourseCommentData, AutoBaseViewHolder> mAdapter;
    private int internalId;

    public static CourseCommentFragment newInstance() {
        CourseCommentFragment fragment = new CourseCommentFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerCourseCommentComponent
                .builder()
                .appComponent(appComponent)
                .courseCommentModule(new CourseCommentModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.courselive_fragment_comment, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        swipeLayout.setOnRefreshListener(() -> getData(true));
        rvList.setAdapter(mAdapter);
        rvList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view);
                if (!mAdapter.getData().isEmpty() && position == mAdapter.getData().size() - 1) {
                    outRect.set(0, 0, 0, RxUtil.getAutoHeight(getActivity(), 270));
                } else {
                    super.getItemOffsets(outRect, view, parent, state);
                }
            }
        });
        mAdapter.setOnLoadMoreListener(()->getData(false),rvList);
        rvList.setLayoutManager(layoutManager);
        getData(true);
    }

    private void getData(boolean pullToRefresh) {
        if (internalId>=0) {
            mPresenter.getComment(pullToRefresh, internalId);
        }else {
            showMessage("数据错误");
        }
    }

    @Override
    public void setData(@Nullable Object data) {
        this.internalId = (int) data;
    }

    @Override
    public void showLoading() {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (swipeLayout != null) {
            swipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

     @Subscriber(tag = RouterHub.COURSELIVE_COURSECOMMENTFRAGMENT)
     public void onEvents(Message msg) {
         switch (msg.what) {
             case EventBusTags.COMMENT_SUCCESS:
                 getData(true);
                 break;
                 default:
         }
     }
    @Override
    public void update(CommonData<List<CourseCommentData>> data) {
        tvCommentNo.setText(data.getCount() + "人");
    }
}
